package com.datastructure.test.maxnumofswap;

public class MaxNumOfSwap {

    public static void main(String[] args) {

    }

    public String maximumSwap (String num) {
        // write code here
        char[] s = num.toCharArray();
        //找到最大索引的位置
        int max = 0;
        for(int i = 1; i < s.length; ++i) {
            if(s[i] >= s[max]) {
                max = i;
            }
        }
        //找到最小的索引的位置
        int idx = 0;
        for(int i = 0; i < s.length; ++i) {
            if(s[i] < s[max]) {
                idx = i;
                break;
            }
        }
        //交换
        if(idx < max) {
            char t = s[idx];
            s[idx] = s[max];
            s[max] = t;
        }
        return new String(s);
    }
}
